package com.yuanli.ad.utils;

import android.app.Activity;
import android.content.Context;
import android.telephony.mbms.DownloadStatusListener;
import android.util.Log;
import android.view.View;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.yuanli.ad.constants.AdConstants;
import com.yuanli.ad.holder.TTAdManagerHolder;

import java.lang.ref.WeakReference;
import java.util.List;

public class InsertAdUtils {
    private Context mContext;
    private TTAdNative mTTAdNative;
    private AdStateListener mAdStateListener;
    private AdLoadListener mAdLoadListener;

    public interface AdStateListener {
        void onSuccses();
        void onError(String msg);
        void onClose();
        void onRenderFail(String msg);
    }

    public void loadInsertAd(Context context,AdStateListener adStateListener) {
        mContext = context;
        AdSlot adSlot = getAdSlot();
        mTTAdNative = TTAdManagerHolder.get().createAdNative(context);
//        mTTAdNative.loadInteractionExpressAd(adSlot,nativeExpressAdListener);
        mAdLoadListener = new AdLoadListener((Activity) mContext,adStateListener);
        mTTAdNative.loadFullScreenVideoAd(adSlot,mAdLoadListener);

    }

    public AdSlot getAdSlot() {
//        return new AdSlot.Builder()
//                .setCodeId(AdConstants.AD_INSERT_ID) //广告位id
//                .setSupportDeepLink(false)
//                .setAdCount(1) //请求广告数量为1到3条
//                .setExpressViewAcceptedSize(300, 450) //期望模板广告view的size,单位dp
//                .setNativeAdType(AdSlot.TYPE_INTERACTION_AD)
//                .build();

        return new AdSlot.Builder()
                .setCodeId(AdConstants.AD_INSERT_ID)
                .setAdLoadType(TTAdLoadType.LOAD)
                .setExpressViewAcceptedSize(300, 450)
//                .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)//设置横竖屏方向
                .build();

    }

    private TTAdNative.NativeExpressAdListener nativeExpressAdListener = new TTAdNative.NativeExpressAdListener() {
        @Override
        public void onError(int i, String s) {
            Log.d("AdUtils" ,"onError"+s);
            if(mAdStateListener != null){
                mAdStateListener.onError(s);
            }
        }

        @Override
        public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
            if (list.get(0) == null) {
                return;
            }
            final TTNativeExpressAd ad = list.get(0);
            ad.showInteractionExpressAd((Activity) mContext);
            ad.render();
            ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
                @Override
                public void onAdDismiss() {
                    if (mAdStateListener != null){
                        mAdStateListener.onClose();
                    }
                }

                @Override
                public void onAdClicked(View view, int i) {
                    Log.d("AdUtils" ,"onAdClicked");
                }

                @Override
                public void onAdShow(View view, int i) {
                    Log.d("AdUtils" ,"onAdShow");
                }

                @Override
                public void onRenderFail(View view, String s, int i) {
                    Log.d("AdUtils" ,"onRenderFail");
                    if(mAdStateListener != null){
                        mAdStateListener.onRenderFail(s);
                    }
                }

                @Override
                public void onRenderSuccess(View view, float v, float v1) {
                    Log.d("AdUtils" ,"onRenderSuccess");
                    if(mAdStateListener != null){
                        mAdStateListener.onSuccses();
                    }
                }
            });
        }
    };

    public void release(){
        mContext = null;
        mAdStateListener = null;
        if (mTTAdNative != null) {
            mTTAdNative = null;
        }
    }


    /**
     * 【必须】广告加载期间生命周期监听
     */

    private static class AdLoadListener implements TTAdNative.FullScreenVideoAdListener {

        private final Activity mActivity;

        private TTFullScreenVideoAd mAd;
        private AdStateListener mAdStateListener;

        public AdLoadListener(Activity activity,AdStateListener ad) {
            mActivity = activity;
            mAdStateListener = ad;
        }

        @Override
        public void onError(int code, String message) {
            Log.e("AdUtils", "Callback --> onError: " + code + ", " + message);
            if(mAdStateListener != null){
                mAdStateListener.onRenderFail(message);
            }
        }

        @Override

        public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
            Log.e("AdUtils", "Callback --> onFullScreenVideoAdLoad");
            handleAd(ad);
        }

        @Override

        public void onFullScreenVideoCached() {
            // 已废弃 请使用 onRewardVideoCached(TTRewardVideoAd ad) 方法
        }

        @Override

        public void onFullScreenVideoCached(TTFullScreenVideoAd ad) {
            Log.e("AdUtils", "Callback --> onFullScreenVideoCached");
            Log.e("AdUtils", "Callback --> onFullScreenVideoCached");
            handleAd(ad);
        }

        /**
         * 处理广告对象
         */

        public void handleAd(TTFullScreenVideoAd ad) {
            if (mAd != null) {
                return;
            }
            mAd = ad;
            //【必须】广告展示时的生命周期监听

            mAd.setFullScreenVideoAdInteractionListener(new AdLifeListener(mActivity,mAdStateListener));
            mAd.showFullScreenVideoAd(mActivity);
        }
    }

    /**
     * 【必须】广告生命状态监听器
     */

    private static class AdLifeListener implements TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {

        private final WeakReference<Context> mContextRef;
        private  AdStateListener mAdStateListener;

        public AdLifeListener(Context context, AdStateListener adStateListener) {
            mContextRef = new WeakReference<>(context);
            mAdStateListener = adStateListener;
        }

        @Override

        public void onAdShow() {
            Log.d("AdUtils", "Callback --> FullVideoAd show");
        }

        @Override

        public void onAdVideoBarClick() {
            Log.d("AdUtils", "Callback --> FullVideoAd bar click");
        }

        @Override

        public void onAdClose() {
            Log.d("AdUtils", "Callback --> FullVideoAd close");
            mAdStateListener.onClose();

        }

        @Override
        public void onVideoComplete() {
            Log.d("AdUtils", "Callback --> FullVideoAd complete");
        }

        @Override
        public void onSkippedVideo() {
            Log.d("AdUtils", "Callback --> FullVideoAd skipped");
            mAdStateListener.onClose();
        }

    }

}
